package com.lvan.controller.common;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lvan.bean.BaseException;
import com.lvan.bean.Goods;
import com.lvan.bean.Page;
import com.lvan.bean.User;
import com.lvan.controller.BaseController;
import com.lvan.service.interfaze.common.IGoodsService;

@Controller
@RequestMapping("/goods")
public class GoodsController extends BaseController{
	@Autowired
	private IGoodsService goodsService;
	@RequestMapping(value="/query")
	public String query(ModelMap model,Page page, Goods g, HttpServletRequest request) {
		List<Goods> ls = goodsService.query(g, page);
		model.put("data", ls);
		return INDEX;
	}
	
	@RequestMapping(value="/querykey")
	public String queryKey(String skey, boolean isBarCode, ModelMap model, Page page, HttpServletRequest request) {
		Goods goods = new Goods();
		goods.setFk(true);
		if(isBarCode) {
			goods.setBarCode(skey);
		}else {
			goods.setFuzzy(skey);
		}
		List<Goods> ls = goodsService.query(goods , page);
		model.put("data", ls);
		return INDEX;
	}
	
	@RequestMapping(value="/delete")
	public void delete(Goods goods, HttpServletRequest request) throws BaseException {
		if(goods.getIds()!=null)
			this.goodsService.delete(goods.getIds());
		else
			this.goodsService.delete(goods.getGoodsId());
	}
	
	@RequestMapping(value="/add")
	public String add(Goods g, HttpServletRequest request) throws BaseException {
		this.goodsService.add(g);
		return INDEX;
	}
	
	@RequestMapping(value="/update")
	public String update(Goods g, HttpServletRequest request) throws BaseException {
		this.goodsService.update(g,(User) request.getSession().getAttribute("user"));
		return INDEX;
	}
}
